using IQIGame.Onigao.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace IQIGame.Onigao.Game
{
    public class FrameworkPath
    {
        private static Dictionary<ValueTuple<string, string>, string> full_path_cache_2 = new();
        private static Dictionary<ValueTuple<string, string, string>, string> full_path_cache_3 = new();

        public static string GetFullPathCache(string prefix, string url)
        {
            ValueTuple<string, string> key = new ValueTuple<string, string>(prefix, url);
            if (!full_path_cache_2.TryGetValue(key, out var value))
            {
                value = prefix + url;
                full_path_cache_2.Add(key, value);
            }
            return value;
        }

        public static string GetFullPathCache(string prefix, string url, string endfix)
        {
            ValueTuple<string, string, string> key = new ValueTuple<string, string, string>(prefix, url, endfix);
            if (!full_path_cache_3.TryGetValue(key, out var value))
            {
                value = prefix + url + endfix;
                full_path_cache_3.Add(key, value);
            }
            return value;
        }


        public const string AssetBundleExtension = ".ab";

        public const string ResABScenePath = "Res/01_Scenes/";
        public const string PrefabPath = "Res/03_Prefabs/";
        public const string UIPrefabPath = "Res/03_Prefabs/UI/";

        public const string AtlasSpritePath = "Res/05_Images/Sprite/";
        public const string TexturePath = "Res/05_Images/Texture/";

        /// <summary>
        /// 编辑器上的音频和视频目录
        /// </summary>
        public const string MediaPath = "Res/21_Media";
        public const string AudioPath = MediaPath + "/Audios/";
        public const string VideoPath = MediaPath + "/Videos/";

        /// <summary>
        /// 真机上的音频和视频目录
        /// </summary>
        public const string PackedMediaPath = "Media";
        public const string PackedMediaConfigJsonFileName = "media_config.txt";
        public const string PackedMediaConfigJsonPath = PackedMediaPath + "/" + PackedMediaConfigJsonFileName;
        public const string PackedAudioPath = PackedMediaPath + "/Audios/";
        public const string PackedVideoPath = PackedMediaPath + "/Videos/";

        /// <summary>
        /// 角色语音资源目录
        /// </summary>
        public const string AudioVoicePath = "Voice/";
        //音频acb文件后缀
        public const string AudioAcbExtension = ".acb";
        //音频awb文件后缀
        public const string AudioAwbExtension = ".awb";
        //视频文件后缀
        public const string VideoExtension = ".usm";

        public static string GetABScenePath(string sceneName)
        {
            return string.Concat(ResABScenePath, sceneName, FramePathConsts.AssetBundleExtension);
        }

        public static string GetUIPrefabPath(string prefabName)
        {
            return string.Concat(UIPrefabPath, prefabName, ".prefab");
        }

        public static string GetAudioFilePath(string filePath, out bool isPersistentPath)
        {
            string path;
#if UNITY_EDITOR
            isPersistentPath = false;
            path = Path.Combine(Application.dataPath, AudioPath, filePath);
#else
            path = Path.Combine(FilePathHelper.Instance.PersistentDataPath, PackedAudioPath, filePath);
            isPersistentPath = true;
            if (!File.Exists(path))
            {
                path = Path.Combine(CriWare.Common.streamingAssetsPath, PackedAudioPath, filePath);
                isPersistentPath = false;
            }
#endif
            return path;
        }

        public static string GetAudioAcbFilePath(string acbName, AudioSourceType sourceType, out bool isPersistentPath)
        {
            string path;
            switch (sourceType)
            {
                case AudioSourceType.BgmSource:
                case AudioSourceType.SfxSource:
                case AudioSourceType.LoopSource:
                case AudioSourceType.Entity:
                    path = GetAudioFilePath(acbName + AudioAcbExtension, out isPersistentPath);
                    break;
                case AudioSourceType.VoiceSource:
                    //case AudioSourceType.Live2DSource:
                    path = GetAudioFilePath(string.Concat(AudioVoicePath, LanguageGlobal.GetVoiceLanguageStr(), "/", acbName + AudioAcbExtension), out isPersistentPath);
                    break;
                default:
                    throw new System.ArgumentOutOfRangeException(nameof(sourceType), sourceType, null);
            }

            return path;
        }

        public static string GetAudioAwbFilePath(string awbName, AudioSourceType sourceType, out bool isPersistentPath)
        {
            string path;
            switch (sourceType)
            {
                case AudioSourceType.BgmSource:
                case AudioSourceType.SfxSource:
                case AudioSourceType.LoopSource:
                case AudioSourceType.Entity:
                    path = GetAudioFilePath(awbName + AudioAwbExtension, out isPersistentPath);
                    break;
                case AudioSourceType.VoiceSource:
                    //case AudioSourceType.Live2DSource:
                    path = GetAudioFilePath(string.Concat(AudioVoicePath, LanguageGlobal.GetVoiceLanguageStr(), "/", awbName + AudioAwbExtension), out isPersistentPath);
                    break;
                default:
                    throw new System.ArgumentOutOfRangeException(nameof(sourceType), sourceType, null);
            }

            return path;
        }

        public static string GetVideoFilePath(string filePath)
        {
            string path;
            var pathWithExt = filePath + VideoExtension;
#if UNITY_EDITOR
            path = Path.Combine(Application.dataPath, VideoPath, pathWithExt);
#else
            path = Path.Combine(FilePathHelper.Instance.PersistentDataPath, PackedVideoPath, pathWithExt);
            if (!File.Exists(path))
            {
                path = Path.Combine(CriWare.Common.streamingAssetsPath, PackedVideoPath, pathWithExt);
            }
#endif
            return path;
        }
    }
}
